package io.wmy.supex.framework.support.spring.view;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import io.wmy.supex.common.bean.MyContent;
import io.wmy.supex.framework.support.spring.view.override.AbstractIText5PdfView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by qiaoming on 2016/8/5.
 */
public class PdfView extends AbstractIText5PdfView {

    @Override
    protected void buildPdfMetadata(Map<String, Object> model, Document document, HttpServletRequest request) {
        Rectangle rec = new Rectangle(600, 800);
        document.setPageSize(rec);
        document.setPageCount(1);
    }

    @Override
    protected void buildPdfDocument(Map<String, Object> model,
                                    Document document, PdfWriter writer, HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        PdfPTable table = new PdfPTable(5);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.getDefaultCell().setBackgroundColor(BaseColor.CYAN);
        Object ob = model.get("contents");
        if (ob instanceof List) {
            for (int i = 0; i < ((List<?>) ob).size(); i++) {
                Object feedObj = ((List<?>) ob).get(i);
                MyContent myContent = (MyContent) feedObj;
                table.addCell(myContent.getId());
                table.addCell(myContent.getTitle());
                table.addCell(myContent.getDescription());
                table.addCell(myContent.getPubDate().toString());
                table.addCell(myContent.getLink());
            }
        }
        table.completeRow();
        document.add(table);
    }
}